<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/25
 * Time: 18:32
 */
namespace app\admin\controller;
use app\admin\model\Weke;
use think\Controller;
use think\Request;

class Wechats extends Controller{
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('wechats/del')
            ->addTableHeaderAdd('wechats/show_add')
            ->addTableColumn('id','text','ID')
            ->addTableColumn('name','text','名称')
            ->addTableColumn('appid','text','微信appid')
            ->addTableColumn('appsecret','text','微信appsecret')
            ->addTableColumn('mch_id','text','微信商户mch_id')
            ->addTableColumn('mch_key','text','微信商户mch_key')
            ->addTableColumn('notify_url','text','微信商户回调')
            ->addTableColumn('fee_type','text','微信支付币种')
//            ->addTableColumn('spbill_create_ip','text','ip')
            ->addTableColumn('status','switch','状态',['默认','关闭'],'wechats/change')
            ->addTableRightHeader('操作')
            ->addTableRight('detail','wechats/detail','查看')
            ->addTableRight('edit','wechats/show_edit','编辑')
            ->addTableRight('del','wechats/del','删除')
            ->addTablePage('wechats/page')
            ->fetch();
    }
    public function page(Request $request){
        $limit = $request->param('limit',10);
        $data = Weke::where('id','>',0)
            ->order('id desc')
            ->paginate($limit)
            ->toArray();
        return json_page_response($data);
    }
    public function change(Request $request){
        $id = $request->param('id');
        $thisshuju = Weke::where('id',$id)->findOrEmpty()->toArray();
        if($thisshuju['status']==1){
            $thisshuju['status'] = 0;
        }else{
            $thisshuju['status'] = 1;
            $shuju = Weke::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                if($value['status']==1){
                    $value['status'] = 0;
                    Weke::where('id',$value['id'])->update(['status'=>0]);
                }
            }
        }
        if(Weke::where('id',$thisshuju['id'])->update(['status'=>$thisshuju['status']])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }
    /**加载添加
     * @param Request $request
     * @return mixed
     */
    public function show_add(Request $request){
        return NewBuilder::make('form')
            ->addAction('wechats/add')
            ->addInput('name','名称')
            ->addInput('appid','微信appid')
            ->addInput('appsecret','微信appsecret')
            ->addInput('mch_id','微信商户mch_id')
            ->addInput('mch_key','微信商户mch_key')
            ->addInput('notify_url','微信商户回调')
            ->addInput('fee_type','微信支付币种','CNY')
            ->addInput('spbill_create_ip','IP','127.0.0.1')
            ->addInput('trade_type','微信交易类型','JSAPI')
            ->addInput('sign_type','签名类型','MD5')
            ->addSwitch('status','是否启用，是否默认')
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
            $shuju = Weke::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                Weke::where('id',$value['id'])->update(['status'=>0]);
            }
        }else{
            $data['status'] = 0;
        }
        if(Weke::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**
     * @param Request $request
     * @return mixed
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Weke::where('id',$id)->findOrEmpty()->toArray();
        return NewBuilder::make('form')
            ->addAction('wechats/edit')
            ->addNone('id',$id)
            ->addInput('name','名称',$data['name'])
            ->addInput('appid','微信appid',$data['appid'])
            ->addInput('appsecret','微信appsecret',$data['appsecret'])
            ->addInput('mch_id','微信商户mch_id',$data['mch_id'])
            ->addInput('mch_key','微信商户mch_key',$data['mch_key'])
            ->addInput('notify_url','微信商户回调',$data['notify_url'])
            ->addInput('fee_type','微信支付币种',$data['fee_type'])
            ->addInput('spbill_create_ip','IP',$data['spbill_create_ip'])
            ->addInput('trade_type','微信交易类型',$data['trade_type'])
            ->addInput('sign_type','签名类型',$data['sign_type'])
            ->addSwitch('status','是否启用，是否默认')
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }
    public function detail(Request $request){
        $id = $request->param('id');
        $data = Weke::where('id',$id)->findOrEmpty()->toArray();
        return NewBuilder::make('form')
            ->addAction('wechats/edit')
            ->addNone('id',$id)
            ->addInput('name','名称',$data['name'])
            ->addInput('appid','微信appid',$data['appid'])
            ->addInput('appsecret','微信appsecret',$data['appsecret'])
            ->addInput('mch_id','微信商户mch_id',$data['mch_id'])
            ->addInput('mch_key','微信商户mch_key',$data['mch_key'])
            ->addInput('notify_url','微信商户回调',$data['notify_url'])
            ->addInput('fee_type','微信支付币种',$data['fee_type'])
            ->addInput('spbill_create_ip','IP',$data['spbill_create_ip'])
            ->addInput('trade_type','微信交易类型',$data['trade_type'])
            ->addInput('sign_type','签名类型',$data['sign_type'])
            ->addSwitch('status','是否启用，是否默认')
//            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**修改
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function edit(Request $request){
        $data = $request->param();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
            $shuju = Weke::where('id','>',0)->select()->toArray();
            foreach ($shuju as $value){
                Weke::where('id',$value['id'])->update(['status'=>0]);
            }
        }else{
            $data['status'] = 0;
        }
        if(Weke::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function del(Request $request){
        $id = $request->param('id');
        if(Weke::where('id','in',$id)->delete()){
            return json_my_response('删除成功',200);
        }else{
            return json_my_response('删除失败',205);
        }
    }
}